﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SYH.Config;
using System.Data;
using Common;

namespace SYH.User.DAL
{
   public  class UserCompanyInfo
    {
       #region 更新
        /// <summary>
        /// 更新详细内容
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
       public bool Update(SYH.User.Model.UserCompanyInfo Model)
        {
            SqlParameter[] par = new SqlParameter[] { 
            new SqlParameter("@UserId",Model.UserId),
            new SqlParameter("@CompanyName",Model.CompanyName),
            new SqlParameter("@CompanyType",Model.CompanyType),
            new SqlParameter("@Pattern",Model.Pattern),
            new SqlParameter("@SellProduct",Model.SellProduct),
            new SqlParameter("@BuyProduct",Model.BuyProduct),
            new SqlParameter("@Telephone",Model.Telephone),
            new SqlParameter("@Mobile",Model.Mobile),
            new SqlParameter("@Fax",Model.Fax),
            new SqlParameter("@OtherTelephone",Model.OtherTelephone),
            new SqlParameter("@ZipCode",Model.ZipCode),
            new SqlParameter("@LinkMan",Model.LinkMan),
            new SqlParameter("@Sex",Model.Sex),
            new SqlParameter("@IsDomain",Model.IsDomain),
            new SqlParameter("@RegMoney",Model.RegMoney),
            new SqlParameter("@RegAddress",Model.RegAddress),
            new SqlParameter("@OperateAddress",Model.OperateAddress),
            new SqlParameter("@FoundTime",Model.FoundTime),
            new SqlParameter("@IndustryId",Model.IndustryId)
            };
            return SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "User_CompanyInfo_Update", par) > 0;
        }

        #endregion

       #region 获取会员注册的公司信息
       /// <summary>
        /// GetModel
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
       public SYH.User.Model.UserCompanyInfo GetModel(int UserID)
        {

            SqlDataReader sdt = Common.SqlHelper.ExecuteReader(WebConfig.SqlConnString, CommandType.StoredProcedure
                , "User_CompanyInfo_GetModel"
                , new SqlParameter("@UserID", UserID));

            if (sdt.HasRows)
            {
               SYH.User.Model.UserCompanyInfo model = new Model.UserCompanyInfo();
                if (sdt.Read())
                {
                    model.UserId = sdt.GetInt32(sdt.GetOrdinal("UserId"));
                    model.BuyProduct = sdt.GetString(sdt.GetOrdinal("BuyProduct"));
                    model.CompanyName = sdt.GetString(sdt.GetOrdinal("CompanyName"));
                    model.CompanyType = sdt.GetInt32(sdt.GetOrdinal("CompanyType"));
                    model.Fax = sdt.GetString(sdt.GetOrdinal("Fax"));
                    model.FoundTime = sdt.GetDateTime(sdt.GetOrdinal("FoundTime"));
                    model.IsDomain = sdt.GetInt32(sdt.GetOrdinal("IsDomain"));
                    model.LinkMan = sdt.GetString(sdt.GetOrdinal("LinkMan"));
                    model.Sex = sdt.GetInt32(sdt.GetOrdinal("Sex"));
                    model.Mobile = sdt.GetString(sdt.GetOrdinal("Mobile"));
                    model.OtherTelephone = sdt.GetString(sdt.GetOrdinal("OtherTelephone"));
                    model.OperateAddress = sdt.GetString(sdt.GetOrdinal("OperateAddress"));
                    model.Pattern = sdt.GetInt32(sdt.GetOrdinal("Pattern"));
                    model.RegAddress = sdt.GetString(sdt.GetOrdinal("RegAddress"));
                    model.RegMoney = sdt.GetInt32(sdt.GetOrdinal("RegMoney"));
                    model.SellProduct = sdt.GetString(sdt.GetOrdinal("SellProduct"));
                    model.Telephone = sdt.GetString(sdt.GetOrdinal("Telephone"));
                    model.ZipCode = sdt.GetString(sdt.GetOrdinal("ZipCode"));
                    model.IndustryId = sdt.GetInt32(sdt.GetOrdinal("IndustryId"));
                    sdt.Close();
                    return model;

                }
            }
            sdt.Close();
            return null;
        }
        #endregion

       #region 根据用户Id 查询用户公司名称
       /// <summary>
       ///  根据用户Id 查询用户公司名称
       /// </summary>
       /// <param name="UserID"></param>
       /// <returns></returns>
       public string GetCompanyName(int UserID)
       {
           var obj = Common.SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, "select CompanyName from User_CompanyInfo where UserId=" + UserID);
           if (obj == null)
           {
               return string.Empty;
           }
           else
           {
               return Convert.ToString(obj);

           }
       }
       #endregion


       #region 根据公司名称查询是否存在此公司
       /// <summary>
       ///  根据公司名称查询是否存在此公司
       /// </summary>
       /// <param name="companyName"></param>
       /// <returns></returns>
       public int CheckCompanyNameExist(string Name)
       {
           return Common.TypeParse.ObjectToInt(SqlHelper.ExecuteScalar(WebConfig.SqlConnString,
               CommandType.StoredProcedure,
               "User_CompanyInfo_CheckCompanyNameExist",
               new SqlParameter("@CompanyName", Name)), 0);
       }
       #endregion

       #region 根据条件获取某一字段
       /// <summary>
       /// 根据条件获取某一字段
       /// </summary>
       /// <param name="fieldName">字段</param>
       /// <param name="strWhere">条件</param>
       public object GetOneFieldValueByWhere(string fieldName, string strWhere)
       {
           if (string.IsNullOrEmpty(fieldName))
           {
               fieldName = "0";
           }
           StringBuilder strSql = new StringBuilder();
           strSql.Append("SELECT Top 1 " + fieldName + " FROM User_CompanyInfo  ");
           strSql.Append("WHERE " + strWhere);
           return SqlHelper.GetSingle(WebConfig.SqlConnString, strSql.ToString());
       }
       #endregion


       #region 根据条件更新多个字段
       /// <summary>
       /// 根据条件更新多个字段
       /// </summary>
       /// <param name="cols">字段列表 以,分隔</param>
       /// <param name="values">字段值列表 以,分隔;与字段对应</param>
       /// <param name="where">条件</param>
       /// <returns></returns>
       public int UpdateFields(string[] cols, object[] values, string where)
       {
           SqlParameter[] ps = new SqlParameter[values.Length];
           for (int i = 0; i < values.Length; i++)
           {
               ps[i] = new SqlParameter(cols[i], values[i] ?? DBNull.Value);
           }
           string[] ufs = new string[cols.Length];
           for (int i = 0; i < cols.Length; i++)
           {
               ufs[i] = cols[i] + "=@" + cols[i];
           }
           string uf = string.Join(",", ufs);
           string sql = string.Format("update User_CompanyInfo  set {0} where {1} ", uf, where);
           return Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, sql, ps);
       }
       #endregion

    }
}
